package com.ruoyi.web.erp.controller;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.framework.web.service.GiteeLoginService;
import com.ruoyi.web.config.GiteeConfig;
import com.ruoyi.web.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class GiteeController {



    @RequestMapping("/auth-redirect")
    public Object giteeLogin() {
        Map<Object,Object> resultMap = new HashMap<>();
        String userStr=GiteeConfig.getCode
                .replace("{client_id}", GiteeConfig.client_id)
                .replace("{redirect_uri}", GiteeConfig.redirect_uri);
        resultMap.put("authorize",userStr);
        return resultMap;
    }

    @RequestMapping("/GiteeCallBack")
    public Object callBack(String code) {
        //1、通过code获取access_token   授权token
        String accessTokenUrl = GiteeConfig.getAccessToken.replace("{code}", code)
                .replace("{client_id}", GiteeConfig.client_id)
                .replace("{redirect_uri}", GiteeConfig.redirect_uri)
                .replace("{client_secret}", GiteeConfig.client_secret);
        try {
            String accessToken = WebUtil.post(accessTokenUrl, null, "UTF-8");
            System.out.println("accessToken:" + accessToken);

            JSONObject temp = JSONObject.parse(accessToken);
            if(temp.getString("access_token")!=null){
                //2、通过  access_token  获取用户信息
                Map<String, String> params = new HashMap<>();
                params.put("access_token", temp.getString("access_token"));
                String userStr= WebUtil.get(GiteeConfig.getUserInfo, params, "UTF-8");

                JSONObject userMap = JSONObject.parse(userStr);
                String userId= userMap.getString("id");
            }





        } catch (IOException e) {
            e.printStackTrace();
        }


        return code;
    }


    @Autowired
    GiteeLoginService giteeLoginService;

    /**
     * 登录方法
     *
     * @param code giteeCode
     * @return 结果
     */
    @PostMapping("/giteeLogin")
    public AjaxResult login(@RequestBody String code)
    {
        AjaxResult ajax = AjaxResult.success();

        String accessTokenUrl=GiteeConfig.getAccessToken;
        //1、通过code获取access_token   授权token
        accessTokenUrl = accessTokenUrl.replace("{code}", JSONObject.parse(code).getString("code"))
                .replace("{client_id}", GiteeConfig.client_id)
                .replace("{redirect_uri}", GiteeConfig.redirect_uri)
                .replace("{client_secret}", GiteeConfig.client_secret);
        try {
            String accessToken = WebUtil.post(accessTokenUrl, null, "UTF-8");
            System.out.println("accessToken:" + accessToken);

            JSONObject temp = JSONObject.parse(accessToken);
            if(temp.getString("access_token")!=null){
                //2、通过  access_token  获取用户信息
                Map<String, String> params = new HashMap<>();
                params.put("access_token", temp.getString("access_token"));
                String userStr= WebUtil.get(GiteeConfig.getUserInfo, params, "UTF-8");

                JSONObject userMap = JSONObject.parse(userStr);
                String userId= userMap.getString("id");
                String userName= userMap.getString("name");

                // 生成令牌
                String token = giteeLoginService.login(userName,userId);
                ajax.put(Constants.TOKEN, token);
            }else{
                ajax=AjaxResult.error();
            }

        } catch (IOException e) {
            ajax=AjaxResult.error();
            e.printStackTrace();
        }
        return ajax;
    }
}
